// 购物车数据action
import { get, post } from '@common/ajax';
import urls from '@common/url';
import { addCityRegionInfo, getProductsList, refreshReviewInvalidList } from '@biztools/shopReview';
import {
    CHANGE_REVIEW_PRODUCT_LIST,
    CHANGE_REVIEW_REMARK,
    SET_REVIEW_ADDRESS,
    GET_ORDER_REQUEST_ID,
    CLEAR_ORDER_REQUEST_ID,
    CHANGE_REVIEW_INVALID_LIST,
} from '../mutationType';
import logger from '@common/logger';
import { ClearTip, Tip, TipLoading } from '@common/tip';
const bag = {
    state: {
        shopReviewAddress: null,
        shopReviewList: [],
        shopReviewRemark: '',
        shopRequestId: '',
        shopReviewInvalidList: [],
    },
    mutations: {
        CHANGE_REVIEW_PRODUCT_LIST: (state, shopReviewList) => {
            Object.assign(state, {
                shopReviewList,
            });
        },
        CHANGE_REVIEW_REMARK: (state, shopReviewRemark) => {
            Object.assign(state, {
                shopReviewRemark,
            });
        },
        SET_REVIEW_ADDRESS: (state, shopReviewAddress) => {
            Object.assign(state, { shopReviewAddress });
        },
        GET_ORDER_REQUEST_ID: (state, shopRequestId) => {
            Object.assign(state, { shopRequestId });
        },
        CLEAR_ORDER_REQUEST_ID: (state) => {
            Object.assign(state, { shopRequestId: '' });
        },
        CHANGE_REVIEW_INVALID_LIST: (state, shopReviewInvalidList) => {
            Object.assign(state, { shopReviewInvalidList });
        },
    },
    actions: {
        // 获取requestId
        getRequestId: async ({ commit, state }) => {
            // if (state.shopRequestId) {
            //     return false;
            // }
            const res = await get(urls.requestId);
            logger.success('ShopReview => 获取到的RequestId为', res);
            commit(GET_ORDER_REQUEST_ID, res.request_id);
            // return true;
        },
        // 支付完成，清除requestid
        clearRequestId: ({ commit }) => {
            commit(CLEAR_ORDER_REQUEST_ID);
        },
        // 设置预览地址
        setShopReviewAddress: ({ commit, dispatch }, address) => {
            commit(SET_REVIEW_ADDRESS, address);
            // dispatch('refreshReviewListWarehouseInfo');
        },
        onlyChagneReviewListData: ({ commit }, list) => {
            commit(CHANGE_REVIEW_PRODUCT_LIST, addCityRegionInfo(list));
        },
        // 修改展示商品数据
        changeShopReviewList: ({ commit, dispatch }, { list, isMain }) => {
            commit(CHANGE_REVIEW_PRODUCT_LIST, addCityRegionInfo(list));
            dispatch('refreshReviewListWarehouseInfo', isMain);
        },
        // 修改备注
        changeShopReviewRemark: ({ commit }, remark) => {
            commit(CHANGE_REVIEW_REMARK, remark);
        },
        // 匹配当前选中的数据仓库配送信息
        refreshReviewListWarehouseInfo: async ({ commit, state }, isMain=false) => {
            const { shopReviewAddress, shopReviewList, shopReviewInvalidList } = state;
            const reviewList = [...shopReviewList, ...shopReviewInvalidList];
            logger.debugger('ShopReview => 刷新仓库信息', shopReviewList, shopReviewInvalidList);
            TipLoading('匹配仓库中...');
            if (!shopReviewAddress) {
                commit(CHANGE_REVIEW_PRODUCT_LIST, reviewList);
                Tip('请先选择地址');
                return;
            }
            const param = {
                lat: shopReviewAddress.latitude,
                lng: shopReviewAddress.longitude,
                city_code: shopReviewAddress.city_code,
                cart_prods: getProductsList(reviewList, isMain),
                // 是否总部优先
                hq_first: 1,
            };
            logger.info('ShopReview => 刷新仓库信息', param);
            const res = await post(urls.shopBagWarehouse, param);
            logger.success('ShopView => 获取的仓库信息为', res);
            // 重新计算数据，计算失效物品
            const { validList, invalidList } = refreshReviewInvalidList(reviewList, res);
            logger.debugger('Debugger => ', invalidList);
            commit(CHANGE_REVIEW_INVALID_LIST, invalidList);
            commit(CHANGE_REVIEW_PRODUCT_LIST, addCityRegionInfo(validList));
            ClearTip();
        },
        // 修改失效产品
        changeInvalidReviewList: ({ commit }, list) => {
            commit(CHANGE_REVIEW_INVALID_LIST, list);
        },
        // 组合下单
        createShopUniteOrder: async ({ commit }, params) => {
            const res = await post(urls.shopCreateOrder, params);
            if (res) {
                return res;
            }
            return false;
        },
        // 下单后支付
        payShopUniteOrder: async ({ commit }, params) => {
            const res = await post(urls.shopUniteOrderPrepay, params);
            return res ? res : false;
        },
    },
};

export default bag;
